// app.js
import store from './store/index';
App({
  store,
  handlerGather:{},
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    const self = this
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // setInterval(() => console.log(this.globalData.shoppingCarCommodityList), 1000*5)
    wx.$on = function (event, fn) {
      if (Array.isArray(event)) {
        event.forEach(item=>{
          wx.$on(item, fn)
        })
      } else {
        (self.handlerGather[event] || (self.handlerGather[event] = []) ).push(fn)
      }
      return wx
    }
    wx.$emit = function (event, data) {
      const fn = (ev, d)=>{
        let len = self.handlerGather && self.handlerGather[ev].length || 0
        for (let i = 0; i < len; i++) {
          const ele = self.handlerGather[ev][i];
          ele(d)
        }
      }
      if (Array.isArray(event)) {
        event.forEach(item=>{
          fn(item,data)
        })
      } else {
        fn(event,data)
      }
      return wx
    }
    wx.$off = function (event) {
      if (!self.handlerGather[event]) return
      if (Array.isArray(event)) {
        event.forEach(item => {
          if (self.handlerGather[event]) {
            self.handlerGather[event] = []
          }
        })
      } else{
        self.handlerGather[event] = []
      }
      return wx
    }


  },
  globalData: {
    // server_ip:'http://192.168.1.115:8081',
    server_ip:'https://www.jianshu.com',
    userInfo: null,
    shareInfo:{ // 分享信息
      title: '校园里的杂货铺',
      desc: '各种好吃的零食，欢迎来购买哦~~',
      path: '/page/index/index'
    },
    // shoppingCarCommodityList: [] // 购物车已选商品
  },
})
